package com.example.learningnote.basic;

import android.util.Log;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

// List是元素有序并且可以重复的集合，称为序列
// List可以精确的控制每个元素的插入位置，或删除某个位置的元素List的两个主要实现类是ArrayList和LinkedList
// - ArrayList底层是由数组实现的
// - 动态增长，以满足应用程序的需求
// - 在列表尾部插入或删除数据非常有效
// - 更适合查找和更新元素
// - ArrayList中的元素可以为nul
public class ListDemo {
    public static void test(){
        // 增加元素
        List list = new ArrayList();
        list.add("Java");
        list.add("Kotlin");
        list.add("Compose");
        list.add("Dart");

        // 查找元素
        Log.d("ListDemo" , "list的元素：" + list.get(1));

        for (int i = 0;i < list.size();i++){
            Log.d("ListDemo" , "第"+ i +"项："  + list.get(i));
        }
        // 修改元素
        list.set(1 , "kotlin");
        Log.d("ListDemo" , "遍历List");
        for (int i = 0;i < list.size();i++){
            Log.d("ListDemo" , "第"+ i +"项："  + list.get(i));
        }
        // 删除元素
        list.remove(1);  // 方法1
        // list.remove("kotlin"); // 方法2
        Log.d("ListDemo" , "遍历List");
        for (int i = 0;i < list.size();i++){
            Log.d("ListDemo" , "第"+ i +"项："  + list.get(i));
        }


        Notice n1 = new Notice(1, "欢迎学习原生安卓开发", "lrq", new Date());
        Notice n2 = new Notice(2, "学习java基础语法", "lrq", new Date());
        Notice n3 = new Notice(3, "学习安卓四大组件", "lrq", new Date());
        Notice n4 = new Notice(4, "学习安卓flutter混合开发", "lrq", new Date());

        ArrayList noticeList = new ArrayList();
        noticeList.add(n1);
        noticeList.add(n2);
        noticeList.add(n3);
        noticeList.add(n4);

        Log.d("ListDemo" , "遍历 noticeList");
        for (int i = 0;i < noticeList.size();i++){
            Notice item = (Notice) noticeList.get(i);
            Log.d("ListDemo" ,  i+1+"：" +item.getTitle());
        }
        Log.d("ListDemo" , "插入一条公告");
        Notice n5 = new Notice(5 , "学习安卓线程进程","lrq" , new Date());
        noticeList.add(3 , n5);
        Log.d("ListDemo" , "遍历 noticeList");
        for (int i = 0;i < noticeList.size();i++){
            Notice item = (Notice) noticeList.get(i);
            Log.d("ListDemo" ,  i+1+"：" +item.getTitle());
        }
        Log.d("ListDemo" , "修改最后一条公告并删除第一条");
        noticeList.remove(0);
        n4.setTitle("学习安卓flutter、rn混合开发");
        Log.d("ListDemo" , "遍历 noticeList");
        for (int i = 0;i < noticeList.size();i++){
            Notice item = (Notice) noticeList.get(i);
            Log.d("ListDemo" ,  i+1+"：" +item.getTitle());
        }


    }
}
